package org.xteam.sled;

import java.io.FileReader;
import java.io.IOException;

import org.xteam.parser.runtime.DefaultErrorReporter;
import org.xteam.sled.ast.SledFile;
import org.xteam.sled.decoder.DecoderBuilder;
import org.xteam.sled.decoder.DecoderBuilder.recognizer;
import org.xteam.sled.parser.SledLexer;
import org.xteam.sled.parser.SledParser;
import org.xteam.sled.semantic.SemanticContext;
import org.xteam.sled.semantic.SledToModel;
import org.xteam.sled.tree.Tree;
import org.xteam.sled.tree.TreeBuilder;
import org.xteam.sled.tree.TreePrinter;


public class TestSled {
	public static void main(String[] args) {
		//String filename = "specs/mips-test.sled";
		//String filename = "specs/mips-core.spec";
		String filename = "specs/mc68000-core.spec";
		//String filename = "specs/pentium-core.spec";
		DefaultErrorReporter reporter = new DefaultErrorReporter(System.out);
		SemanticContext context = null;
		try {
			SledParser parser = new SledParser(new SledLexer(new FileReader(filename), reporter), reporter);
			SledFile file = (SledFile) parser.parse().value;
			SledToModel analyser = new SledToModel(reporter);
			context = analyser.analyse(file);
		} catch (Exception e) {
			if (! "parse error".equals(e.getMessage())) {
				e.printStackTrace();
				return;
			}
		}
		
		if (reporter.hasErrors()) {
			try {
				reporter.printDiagnostic(filename);
			} catch (IOException e2) {
			}
		}
		if (context != null) {
			DecoderBuilder builder = new DecoderBuilder();
			for (recognizer rec : builder.decoder(context)) {
				Tree t = new TreeBuilder().mkTree(rec.getMatch());
				new TreePrinter().print(t);
			}
		}
	}
}
